package com.isoftstone.wms.controller;

import com.isoftstone.wms.entity.Menu;
import com.isoftstone.wms.common.Result;
import com.isoftstone.wms.service.impl.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2025-10-02
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public Result list(@RequestParam String menuRight) {
        try {
            List<Menu> list = menuService.lambdaQuery()
                    .apply("FIND_IN_SET({0}, menuRight)", menuRight)
                    .list();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询菜单失败: " + e.getMessage());
        }
    }

    // 新增菜单
    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        try {
            // 验证必要字段
            if (menu.getMenucode() == null || menu.getMenucode().trim().isEmpty()) {
                return Result.fail("菜单编码不能为空");
            }
            if (menu.getMenuname() == null || menu.getMenuname().trim().isEmpty()) {
                return Result.fail("菜单名称不能为空");
            }
            if (menu.getMenuclick() == null || menu.getMenuclick().trim().isEmpty()) {
                return Result.fail("点击路径不能为空");
            }
            if (menu.getMenucomponent() == null || menu.getMenucomponent().trim().isEmpty()) {
                return Result.fail("组件名称不能为空");
            }
            
            boolean success = menuService.save(menu);
            if (success) {
                return Result.success(menu, "菜单添加成功");
            } else {
                return Result.fail("菜单添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("菜单添加失败: " + e.getMessage());
        }
    }

    // 修改菜单
    @PostMapping("/mod")
    public Result mod(@RequestBody Menu menu) {
        try {
            if (menu.getId() == null) {
                return Result.fail("菜单ID不能为空");
            }
            
            // 验证必要字段
            if (menu.getMenucode() == null || menu.getMenucode().trim().isEmpty()) {
                return Result.fail("菜单编码不能为空");
            }
            if (menu.getMenuname() == null || menu.getMenuname().trim().isEmpty()) {
                return Result.fail("菜单名称不能为空");
            }
            if (menu.getMenuclick() == null || menu.getMenuclick().trim().isEmpty()) {
                return Result.fail("点击路径不能为空");
            }
            if (menu.getMenucomponent() == null || menu.getMenucomponent().trim().isEmpty()) {
                return Result.fail("组件名称不能为空");
            }
            
            boolean success = menuService.updateById(menu);
            if (success) {
                return Result.success(menu, "菜单修改成功");
            } else {
                return Result.fail("菜单修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("菜单修改失败: " + e.getMessage());
        }
    }

    // 删除菜单
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        try {
            if (id == null) {
                return Result.fail("菜单ID不能为空");
            }
            
            boolean success = menuService.removeById(id);
            if (success) {
                return Result.success("菜单删除成功");
            } else {
                return Result.fail("菜单删除失败，可能菜单不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("菜单删除失败: " + e.getMessage());
        }
    }

    // 新增或修改菜单
    @PostMapping("/saveOrMod")
    public Result saveOrMod(@RequestBody Menu menu) {
        try {
            boolean success = menuService.saveOrUpdate(menu);
            if (success) {
                return Result.success(menu, "操作成功");
            } else {
                return Result.fail("操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败: " + e.getMessage());
        }
    }
}